package com.tgh.gles2.c7.textureRect;

import com.tgh.gles2.R;
import com.tgh.gles2.c5.sphereworld.SphereWorldTextureRenderer;
import com.tgh.utils.GLShaderManager;
import com.tgh.utils.GLTools;
import com.tgh.utils.Matrix44f;
import com.tgh.utils.GLShaderManager.ShaderType;

import android.content.Context;
import android.opengl.GLES20;
import java.nio.FloatBuffer;
import javax.microedition.khronos.opengles.GL10;

public class SphereWorldRemarkRenderer extends SphereWorldTextureRenderer {

    public SphereWorldRemarkRenderer(Context context) {
        super(context);
    }
    
    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        super.onSurfaceChanged(gl, width, height);
        initLogo(width, height);
        screenMatrix = new Matrix44f();
        screenMatrix.loadOrtho(0, width, 0,  height, -1.0f, 1.0f);
    }
    @Override
    public void onDrawFrame(GL10 gl) {
        super.onDrawFrame(gl);
        GLES20.glEnable(GLES20.GL_BLEND);
        GLES20.glDisable(GLES20.GL_DEPTH_TEST);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, logoTexture);
        GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
        shaderManager.useStockShader(ShaderType.TEXTURE_REPLACE, screenMatrix.getArray(),0);
        drawLogo();
        GLES20.glDisable(GLES20.GL_BLEND);
        GLES20.glEnable(GLES20.GL_DEPTH_TEST);
    }
    private void initLogo(int w,int h) {
        int[] textures = GLTools.genTextures(1);
        logoTexture=textures[0];
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, logoTexture);
        GLTools.loadDrawableToTexture2D(mContext, 
                R.drawable.opengl_logo, 
                GLES20.GL_NEAREST, 
                GLES20.GL_CLAMP_TO_EDGE);
        
        float logoW=300;
        float logoH=155;
        float x=w-logoW;
        float[] logoVertex={
                x,logoH,
                x,0,
                x+logoW,0,
                x+logoW,logoH
        };
        logoVertices = GLTools.makeFloatBuffer(logoVertex);
        float[] array={
                0,1,
                0,0,
                1,0,
                1,1
        };
        logoTexCoords = GLTools.makeFloatBuffer(array);
    }
    
    private void drawLogo(){
        GLES20.glVertexAttribPointer(GLShaderManager.GL_ATTRIBUTE_VERTEX, 2, GLES20.GL_FLOAT, false, 0, logoVertices);
        GLES20.glEnableVertexAttribArray(GLShaderManager.GL_ATTRIBUTE_VERTEX);
        GLES20.glVertexAttribPointer(GLShaderManager.GL_ATTRIBUTE_TEXTURE0, 2, GLES20.GL_FLOAT, false, 0, logoTexCoords);
        GLES20.glEnableVertexAttribArray(GLShaderManager.GL_ATTRIBUTE_TEXTURE0);
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 4);
    }
    
    private int logoTexture;
    private FloatBuffer logoVertices,logoTexCoords;
    private Matrix44f screenMatrix;
}
